package com.imagination.applet.controller.assembler;

import com.google.common.collect.Lists;
import com.imagination.applet.controller.api.response.AIImagesOfChannelResponse;
import com.imagination.applet.controller.api.response.AIImagesOfTopicResponse;
import com.imagination.applet.controller.view.AIImageVO;
import com.imagination.applet.controller.view.AIImagesOfTopicVO;
import com.imagination.applet.controller.web.configuration.UserHolder;
import com.imagination.applet.domain.aiimage.model.AIImage;
import com.imagination.applet.domain.aiimage.model.TopicType;
import com.imagination.applet.domain.product.repository.ProductImageRepository;
import com.imagination.applet.domain.topic.model.Topic;
import com.imagination.applet.domain.topic.repository.TopicRepository;
import com.imagination.applet.domain.user_topic.model.UserTopic;
import com.imagination.applet.domain.user_topic.repository.UserTopicRepository;
import com.imagination.applet.infrastructure.common.CommonProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class AIImagesViewBuilder {

    private final TopicRepository topicRepository;

    private final UserTopicRepository userTopicRepository;

    private final CommonProperties commonProperties;

    public AIImagesOfChannelResponse buildChannelVO(List<AIImage> images, String channel) {
        Map<String, List<AIImage>> imageListByTopic = images.stream().collect(Collectors.groupingBy(AIImage::getTopicType));
        List<AIImagesOfTopicVO> result = Lists.newArrayList();
        List<Topic> all = topicRepository.findAll(Example.of(Topic.builder().channelType(channel).build())).stream().sorted(Comparator.comparingInt(Topic::getSort)).collect(Collectors.toList());
//        Long userId = 10000015L;
        Long userId = UserHolder.getUserIdNotException();
        for (Topic topic : all) {
            List<AIImage> img = imageListByTopic.get(topic.getCode());
            if (CollectionUtils.isEmpty(img)) {
                img = Lists.newArrayList();
            }
            boolean hadBuy = false;

            if(userId!=null){
                Optional<UserTopic> userTopic = userTopicRepository.findOne(Example.of(UserTopic.builder().userId(userId).topicId(topic.getId()).build()));
                if(userTopic.isPresent()){
                    hadBuy = true;
                }
            }

            if(commonProperties.getFlag()){
                hadBuy = true;
            }

            result.add(AIImagesOfTopicVO.builder()
                    .topic(topic.getCode())
                    .name(topic.getName())
                    .id(topic.getId())
                    .hadBuy(hadBuy)
                    .topicFee(topic.getFee())
                    .imageVOList(img.stream()
                            .map(i -> AIImageVO.builder()
                                    .imageId(i.getId())
                                    .link(i.getLink())
                                    .name(i.getName())
                                    .build())
                            .toList())
                    .build());
        }

        return AIImagesOfChannelResponse.builder()
                .channel(channel)
                .topicImagesList(result)
                .build();
    }

    public AIImagesOfTopicResponse buildTopicVO(Page<AIImage> paged, String channel, String topic) {
        return AIImagesOfTopicResponse.builder()
                .size(paged.getSize())
                .page(paged.getNumber())
                .totalElements(paged.getTotalElements())
                .topic(topic)
                .channel(channel)
                .content(paged.getContent().stream()
                        .map(i -> AIImageVO.builder()
                                .imageId(i.getId())
                                .link(i.getLink())
                                .name(i.getName())
                                .build())
                        .toList()
                )
                .build();
    }
}
